package com.linq.cool.beauty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.beauty.domain.BeautyCartItem;
import com.linq.cool.beauty.domain.BeautyProduct;
import com.linq.cool.beauty.domain.BeautyProductCollection;
import com.linq.cool.beauty.dto.BeautyCartItemDTO;
import com.linq.cool.beauty.mapper.BeautyCartItemMapper;
import com.linq.cool.beauty.repository.BeautyProductCollectionRepository;
import com.linq.cool.beauty.service.BeautyCartItemService;
import com.linq.cool.beauty.service.BeautyProductService;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.exception.BusinessException;
import com.linq.cool.common.core.utils.BeanUtils;
import com.linq.cool.common.core.utils.IdWorker;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.core.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yqlin
 * @Date: 2021/2/8 22:19
 * @Description:
 * @Version: 1.0.0
 */

@Service
public class BeautyCartItemServiceImpl extends ServiceImpl<BeautyCartItemMapper, BeautyCartItem> implements BeautyCartItemService {
    @Autowired
    private BeautyProductService beautyProductService;
    @Autowired
    private BeautyProductCollectionRepository productCollectionRespository;

    @Override
    public boolean saveCart(BeautyCartItemDTO dto) {
        // 根据商品id查询商品信息
        BeautyProduct product = beautyProductService.getById(dto.getProductId());
        if (null == product) {
            throw new BusinessException("该商品已下架");
        }
        BeautyCartItem cartItem = new BeautyCartItem();
        BeanUtils.copyProperties(dto, cartItem);
        cartItem.setMemberId(SecurityUtils.getUserId());
        // 根据客户编号和商品编号查询当前用户是否已经将该商品加入购物车
        BeautyCartItem one = this.getOne(new LambdaQueryWrapper<BeautyCartItem>()
                                                 .eq(BeautyCartItem::getProductId, dto.getProductId())
                                                 .eq(BeautyCartItem::getMemberId, SecurityUtils.getUserId())
        );
        if (null != one) {
            throw new BusinessException("已加入购物车", Result.SUCCESS);
        }
        // 添加购物车
        cartItem
                .setProductPoint(product.getPoint())
                .setProductPrice(product.getPrice())
                .setProductName(product.getName())
                .setProductPic(product.getPic());
        return this.save(cartItem);
    }

    @Override
    public void removeToCollection(List<Long> cartItemIds) {
        // 查询出所有商品id
        List<BeautyCartItem> cartItemList = this.listByIds(cartItemIds);
        List<Long> productIds = cartItemList.stream().map(BeautyCartItem::getProductId).collect(Collectors.toList());
        // 查询出所有商品信息
        List<BeautyProduct> productList = beautyProductService.listByIds(productIds);
        // 查询商品收藏情况
        List<BeautyProductCollection> curProductCollections = productCollectionRespository.findByProductIdInAndAndMemberId(productIds, SecurityUtils.getUserId());
        if (CollectionUtils.isNotEmpty(curProductCollections)) {
            curProductCollections = new ArrayList<>(0);
        }
        // 构造收藏信息
        List<BeautyProductCollection> finalCurProductCollections = curProductCollections;
        List<BeautyProductCollection> resultList = new ArrayList<>();
        productList.forEach(p -> {
            BeautyProductCollection productCollection = new BeautyProductCollection();
            productCollection
                    .setId(SpringUtils.getBean(IdWorker.class).nextId())
                    .setProductId(p.getId())
                    .setMemberId(SecurityUtils.getUserId());
            // 判断当前商品在curProductCollections存在不 存在的话不存  不存在 再存进去
            if (!finalCurProductCollections.contains(productCollection)) {
                productCollection.setProductBrandName(p.getBrandName())
                        .setProductBrandId(p.getBrandId())
                        .setProductPrice(p.getPrice())
                        .setProductCategoryId(p.getProductCategoryId())
                        .setProductPic(p.getPic())
                        .setCreateTime(new Date().toString());
                resultList.add(productCollection);
            }
        });
        // 存库
        productCollectionRespository.saveAll(resultList);
        // 清空当前购物车
        this.removeByIds(cartItemIds);
    }
}
